import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Gao zhao xu
 * Date: 2023-10-13
 * Time: 13:40
 */
public class test2 {
    public static int[] firstMin(int[] arr){
        Stack<Integer> stack= new Stack<>();
        int[] ret = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            int x = -1;
            while (!stack.empty()){
                 if (stack.peek() >= arr[i]){
                     stack.pop();
                 }else{
                     x = stack.peek();
                     break;
                 }
            }
            stack.push(arr[i]);
            ret[i] = x;
        }
        return ret;
    }
    public static void main(String[] args) {
        int[] a = {4,3, 6, 5, 3, 8, 7};
        int[] ret = firstMin(a);
        for (int x: ret) {
            System.out.print(x + " ");
        }
        System.out.println();
    }
}
